package com.wbao.dianniu.ui;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.AnonymousAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AmDataChangeManager;
import com.wbao.dianniu.manager.AmDataChangeManager.IAmDataListener;
import com.wbao.dianniu.manager.MyPartinQAManager;
import com.wbao.dianniu.utils.Utils;

/**
 * 参与的匿名
 * 
 * @author yejianjun
 *
 */
public class PartInAmFragment extends Fragment  implements IQaListener,OnClickListener, IAmDataListener {
	private TextView noDataTV;
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private int currentPage = 0;
	private MyPartinQAManager manager;
	private int TYPE = 41;
	private final int PAGE_COUNTS = 10;
	private AnonymousAdapter adapter;
	private List<QaResponse> mList;
	
	public static PartInAmFragment instance(Context context){
		PartInAmFragment view = new PartInAmFragment();
		return view;
	}
	
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_partin_qa, null);
		adapter =new AnonymousAdapter(view.getContext(),2);
		AmDataChangeManager.getInstance().addAmChangeListener(this);
		initView(view);
		requestData();
		return view;
	}
	private void requestData(){
		manager = new MyPartinQAManager(getActivity());
		manager.addQaListener(this);
		reqData(currentPage);
	}
	private void initView(View view){
		noDataTV = (TextView) view.findViewById(R.id.no_find_data);
		
		pullListView = (PullToRefreshListView) view.findViewById(R.id.list_partin_qa);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Utils.intoAmDetailActivity(getContext(), (QaResponse)adapter.getItem(position-1));
			}
		});
	}
	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		++currentPage;
		reqData(currentPage);
	}
	private  void reqData(int page){
		manager.reqData(GlobalContext.accountId, "", TYPE, page * PAGE_COUNTS , PAGE_COUNTS);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		AmDataChangeManager.getInstance().removeAmChangeListener(this);
		if(null != manager){
			manager.removeQaListener(this);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

	@Override
	public void onQaFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false,false);
	}

	@Override
	public void onQaSuccess(List<QaResponse> list) {
		pullListView.onRefreshComplete(false,false);
		if(null != list){
			if(0 != currentPage){
				adapter.addDataList(list);
			}else{
				adapter.setData(list);
			}
			mList = adapter.getList();
			adapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				noDataTV.setVisibility(View.VISIBLE);
			} else {
				noDataTV.setVisibility(View.GONE);
			}
		}
	}
	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("PartinAnonyFragment");
	}
	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("PartinAnonyFragment");
	}

	@Override
	public void addOneData(QaResponse addData) {
		// TODO Auto-generated method stub
	}

	@Override
	public void removeOneData(int questId) {
		// TODO Auto-generated method stub
		adapter.removeOneData(questId);
		mList = adapter.getList();
		if (mList.size() == 0) {
			noDataTV.setVisibility(View.VISIBLE);
		} else {
			noDataTV.setVisibility(View.GONE);
		}
	}

	@Override
	public void priseNotify(int questId, int isGood, int goodCounts) {
		// TODO Auto-generated method stub
		adapter.priseNotify(questId, isGood, goodCounts);
	}

	@Override
	public void replyNotify(int questId, int answerCounts) {
		// TODO Auto-generated method stub
		adapter.replyNotify(questId, answerCounts);
	}
}
